<style scoped>
    .company >>> .layui-form-select {
        z-index: auto;
    }
</style>

<template>
    <div class="company">
        <fieldset class="layui-elem-field layui-field-title">
            <legend>修改产品价格</legend>
        </fieldset>

        <form class="layui-form " >
            <input type="hidden" name="guid" placeholder="" autocomplete="off" :value="guid" class="layui-input">
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">品种</label>
                <div class="layui-input-block">
                    <select  type="text" name="productguid" id="productguid" lay-filter="productguid" autocomplete="off"   lay-verify="required" class="layui-select" lay-search>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">产品</label>
                <div class="layui-form layui-input-block" lay-filter="salesspecificationid">
                    <select type="text" name="salesspecificationid" id="salesspecificationid" autocomplete="off"   lay-verify="required" class="layui-select" lay-search>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">销售价格</label>
                <div class="layui-input-block">
                    <input type="text" name="price" :value="adata.price" required lay-verify="required"  placeholder="" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">工厂价格</label>
                <div class="layui-input-block">
                    <input type="text" name="factoryprice" :value="adata.factoryprice" required lay-verify="required"  placeholder="" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">币种</label>
                <div class="layui-input-block">
                    <select type="text" name="currency" id="currency" :value="adata.currency"  lay-filter="currency"  autocomplete="off" lay-verify="required" class="layui-select" >
                        <option v-for="(currency, index) in currencys" :value="currency.dictkey">{{currency.dicttext}}</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">计价方式</label>
                <div class="layui-input-block">
                    <select type="text" name="valuation" id="valuation" :value="adata.valuation"  lay-filter="valuation"  autocomplete="off" lay-verify="required" class="layui-select">
                        <option v-for="(valuation, index) in valuations" :value="valuation.dictkey">{{valuation.dicttext}}</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="submit">立即提交</button>
                    <button class="layui-btn layui-btn-primary" @click="$parent.back">返回</button>
                </div>
            </div>
            <div style="height: 200px"></div>



        </form>
    </div>
</template>

<script>
    let qs = require('qs');
    export default {
        name: 'editproductprice',
        props: ['id'],
        data () {
            return {
                guid : this.id,
                adata: {},
                productTypes: [],
                valuations:[ {dicttext:"按件",dictkey:1},{dicttext:"按吨",dictkey:2},],
                currencys:[ {dicttext:"人民币",dictkey:1},{dicttext:"美元",dictkey:2},]
            }
        },
        mounted() {
            let that = this;
            this.getProductTypes();
            layui.use(['form'], function () {
                var form = layui.form,
                    layer = layui.layer;
                form.render();
                that.loadProducts();
                form.on('submit(submit)', function (data) {
                    let loader = layer.load();
                    $.ajax({
                        url: '/sales/Productprice!update.action',
                        data: data.field,
                        type: "post",
                        dataType: "json",
                        success: function(res) {
                            if (res.code == 1) {
                                that.toast("添加成功");
                                that.$parent.switchPage('productprice');
                            } else {
                                that.toast(res.msg);
                            }
                        },
                        complete: function () {
                            layer.close(loader);
                        }
                    });
                    return false;
                });
            });
            layui.use(['form'], function () {
                var form = layui.form;
                form.on('select(productguid)', function(data){
                    that.loadSalesspecification(data.value)
                    form.render('select');//select是固定写法 不是选择器
                });
            });
            $.ajax({
                url: '/sales/Productprice!get.action',
                data: {guid: that.guid},
                type: "post",
                dataType: "json",
                success: function(res) {
                    if (res.info != null) {
                        that.adata = res.info;
                        that.loadSalesspecification(that.adata.productguid);
                        form.render('select');
                    } else {
                        that.toast("系统错误");
                    }
                },
                complete: function () {
                    layer.close(loader);
                }
            });
        },
        updated() {
            layui.form.render('select');
            layui.form.render();
        },
        methods: {
            //加载用户权限信息
            loadProducts:  function (){
                let that = this;
                $("#productguid").empty();
                $('#productguid').append(new Option('请选择', ""));// 下拉菜单里添加元素。
                $.ajax({
                    url: '/sales/Productprice!getBaseInfo.action',
                    type: "post",
                    dataType: "json",
                    success: function(res) {
                        if (res.info != 'error')  {
                            $.each(res.info, function (index, item) {
                                $('#productguid').append(new Option(item.productname , item.guid));
                            });
                            $('#productguid').val(that.adata.productguid);
                            layui.form.render("select");
                        } else {
                            alert("系统错误");
                        }
                    }
                });
            },
            //加载用户权限信息
            loadSalesspecification:  function (val = ''){
                let that = this;
                $("#salesspecificationid").empty();
                $('#salesspecificationid').append(new Option('请选择', ""));// 下拉菜单里添加元素。
                $.ajax({
                    url: '/sales/Salesspecification!getBase.action',
                    data: {productGuid:val},
                    type: "post",
                    dataType: "json",
                    success: function(res) {
                        if (res.info != 'error')  {
                            $.each(res.info, function (index, item) {
                                $('#salesspecificationid').append(new Option(item.name , item.guid));
                            });
                            $('#salesspecificationid').val(that.adata.salesspecificationid);
                            layui.form.render("select", 'salesspecificationid');
                        } else {
                            alert("系统错误");
                        }
                    }
                });
            },
            getProductTypes: function() {
                this.$post('/sales/Sellform!findDict.action', qs.stringify({type: 91})).then(response => {
                    if (response.statusText == "OK") {
                        this.productTypes = response.data;
                    } else {
                        this.toastText('获取产品类型数据失败');
                    }
                });
            }
        }
    }
</script>
